<!DOCTYPE html>












  


<html class="theme-next pisces use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2"/>
<meta name="theme-color" content="#222">






  
  
    
      
    
    
      
    
  <script src="https://cdn.bootcss.com/pace/1.0.2/pace.min.js"></script>
  <link href="https://cdn.bootcss.com/pace/1.0.2/themes/blue/pace-theme-flash.min.css" rel="stylesheet">







<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />






















  

<link href="https://cdn.bootcss.com/font-awesome/4.6.2/css/font-awesome.min.css" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=6.3.0" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=6.3.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=6.3.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=6.3.0">


  <link rel="mask-icon" href="/images/logo.svg?v=6.3.0" color="#222">









<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '6.3.0',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="vue、react隐式实例化 写这篇的缘由是因为上一篇vue与react组件对比学习写的有点啰嗦也没有写的很明白同时也存在一点错误，所以重新写一篇简介概要点的。  隐式实例化 隐式实例化，不希望写在react的jsx里或是vue的template，而是希望通过手动去实例化一个react或是vue组件。  说的有点绕，这种隐式实例化的应用场景在提示信息(message)、模态框(modal)、加载条">
<meta name="keywords" content="react,vue">
<meta property="og:type" content="article">
<meta property="og:title" content="vue、react隐式实例化">
<meta property="og:url" content="http://yoursite.com/2018/08/03/vue、react隐式实例化/index.html">
<meta property="og:site_name" content="三月的米店">
<meta property="og:description" content="vue、react隐式实例化 写这篇的缘由是因为上一篇vue与react组件对比学习写的有点啰嗦也没有写的很明白同时也存在一点错误，所以重新写一篇简介概要点的。  隐式实例化 隐式实例化，不希望写在react的jsx里或是vue的template，而是希望通过手动去实例化一个react或是vue组件。  说的有点绕，这种隐式实例化的应用场景在提示信息(message)、模态框(modal)、加载条">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://yoursite.com/2018/08/03/vue、react隐式实例化/596444702.jpg">
<meta property="og:updated_time" content="2018-11-29T02:09:38.106Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="vue、react隐式实例化">
<meta name="twitter:description" content="vue、react隐式实例化 写这篇的缘由是因为上一篇vue与react组件对比学习写的有点啰嗦也没有写的很明白同时也存在一点错误，所以重新写一篇简介概要点的。  隐式实例化 隐式实例化，不希望写在react的jsx里或是vue的template，而是希望通过手动去实例化一个react或是vue组件。  说的有点绕，这种隐式实例化的应用场景在提示信息(message)、模态框(modal)、加载条">
<meta name="twitter:image" content="http://yoursite.com/2018/08/03/vue、react隐式实例化/596444702.jpg">






  <link rel="canonical" href="http://yoursite.com/2018/08/03/vue、react隐式实例化/"/>



<script type="text/javascript" id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>vue、react隐式实例化 | 三月的米店</title>
  









  <noscript>
  <style type="text/css">
    .use-motion .motion-element,
    .use-motion .brand,
    .use-motion .menu-item,
    .sidebar-inner,
    .use-motion .post-block,
    .use-motion .pagination,
    .use-motion .comments,
    .use-motion .post-header,
    .use-motion .post-body,
    .use-motion .collection-title { opacity: initial; }

    .use-motion .logo,
    .use-motion .site-title,
    .use-motion .site-subtitle {
      opacity: initial;
      top: initial;
    }

    .use-motion {
      .logo-line-before i { left: initial; }
      .logo-line-after i { right: initial; }
    }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">三月的米店</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
      
        <h1 class="site-subtitle" itemprop="description">今儿不吃饭</h1>
      
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">
    <a href="/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">
    <a href="/tags/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />标签</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">
    <a href="/categories/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-th"></i> <br />分类</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">
    <a href="/archives/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档</a>
  </li>

      
      
    </ul>
  

  

  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/08/03/vue、react隐式实例化/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Mark Lee">
      <meta itemprop="description" content="折腾小久的博客,在 2018-08-01 终于基本成型啦，以后就常驻啦">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="三月的米店">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">vue、react隐式实例化
              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2018-08-03 09:33:40" itemprop="dateCreated datePublished" datetime="2018-08-03T09:33:40+08:00">2018-08-03</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2018-11-29 10:09:38" itemprop="dateModified" datetime="2018-11-29T10:09:38+08:00">2018-11-29</time>
              
            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/前端/" itemprop="url" rel="index"><span itemprop="name">前端</span></a></span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="vue、react隐式实例化"><a href="#vue、react隐式实例化" class="headerlink" title="vue、react隐式实例化"></a>vue、react隐式实例化</h1><blockquote>
<p>写这篇的缘由是因为上一篇<a href="https://github.com/limengke123/my-note/blob/master/%E5%89%8D%E7%AB%AF/vue%E4%B8%8Ereact%E7%BB%84%E4%BB%B6%E5%AF%B9%E6%AF%94%E5%AD%A6%E4%B9%A0.md" target="_blank" rel="noopener">vue与react组件对比学习</a>写的有点啰嗦也没有写的很明白同时也存在一点错误，所以重新写一篇简介概要点的。</p>
</blockquote>
<h2 id="隐式实例化"><a href="#隐式实例化" class="headerlink" title="隐式实例化"></a>隐式实例化</h2><blockquote>
<p>隐式实例化，不希望写在<code>react</code>的<code>jsx</code>里或是<code>vue</code>的<code>template</code>，而是希望通过手动去实例化一个<code>react</code>或是<code>vue</code>组件。</p>
</blockquote>
<p>说的有点绕，这种<em>隐式实例化</em>的应用场景在提示信息(message)、模态框(modal)、加载条(loadingbar)，例如一个<code>ajax</code>请求，在成功或失败的时候需要给一个提示：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 下面是伪代码...</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> message <span class="keyword">from</span> <span class="string">'message'</span></span><br><span class="line"></span><br><span class="line">fetch(<span class="string">'/api/xxx'</span>)</span><br><span class="line">    .then(<span class="function"><span class="params">resp</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">if</span>(resp.success === <span class="literal">true</span>)&#123;</span><br><span class="line">            message(&#123;</span><br><span class="line">                type:<span class="string">"success"</span>,</span><br><span class="line">                text:<span class="string">"请求成功"</span></span><br><span class="line">            &#125;)</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            message(&#123;</span><br><span class="line">                type:<span class="string">"error"</span>,</span><br><span class="line">                text:<span class="string">"请求出错"</span></span><br><span class="line">            &#125;)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br></pre></td></tr></table></figure>
<p>可以看的出来，我们的需求是想有一个组件能像<code>html</code>原生的<code>alert</code>一样，在需要的地方能够直接去调用，而不是需要把<code>message</code>组件写进节点中。</p>
<h2 id="难点"><a href="#难点" class="headerlink" title="难点"></a>难点</h2><p>我们都知道不论是<code>react</code>、还是<code>vue</code>也好，写的都是一个类或者叫构造器：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">// react 引用代码省略</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">Message</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span></span>&#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// vue</span></span><br><span class="line"><span class="comment">// export default &#123;</span></span><br><span class="line">    data()&#123;&#125;,</span><br><span class="line">    props:&#123;&#125;,</span><br><span class="line">    methods:&#123;&#125;,</span><br><span class="line">    render:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><code>react</code>相当明显地创建了一个<code>class</code>，<code>vue</code>表面上好像只是暴露一个对象，实际上<code>vue</code>的组件被使用时，你需要把子组件传入父组件的<code>component</code>的对象中，所以<code>Vue</code>会调用<code>Vue.extend({...自组建的选项对象})</code>，这个方法就返回了一个构造器。</p>
<p>既然知道了子组件是一个构造器，那我能不能直接去手动<code>new</code>一个子组件呢？在我测试下来，好像是不行。</p>
<h2 id="vue实例化"><a href="#vue实例化" class="headerlink" title="vue实例化"></a>vue实例化</h2><p>那么如何实例化呢？分成两部分，先是实例化一个组件，然后再把实例化后的组件挂载到html中去。先拿<code>vue</code>说：</p>
<h3 id="拿到构造器"><a href="#拿到构造器" class="headerlink" title="拿到构造器"></a>拿到构造器</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">'vue'</span></span><br><span class="line"><span class="keyword">import</span> message <span class="keyword">from</span> <span class="string">'./message.vue'</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 注意： 这里的message仅仅就是一个对象，需要转成构造器</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> messageConstructor = Vue.extend(message)</span><br></pre></td></tr></table></figure>
<h3 id="实例化"><a href="#实例化" class="headerlink" title="实例化"></a>实例化</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> customProps = &#123;</span><br><span class="line">    <span class="comment">// 传给组件的一些props</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 这样就能拿到了一个vue组件的实例，就能做很多事情了，比如调用实例中写好的methods中的方法，当然这还没完，我们还得把实例挂载到Html中</span></span><br><span class="line"><span class="keyword">const</span> messageInstance = <span class="keyword">new</span> messageConstuctor(&#123;<span class="attr">propsData</span>:customProps&#125;)</span><br></pre></td></tr></table></figure>
<h3 id="挂载"><a href="#挂载" class="headerlink" title="挂载"></a>挂载</h3><p><code>vue</code>的实例有一个很重要的方法：<code>$mount</code>，在选项对象中我们没有传入<code>el</code>属性，所以你在这里手动实例化的<code>vue</code>实例是没有挂载出来的，需要手动调用一遍<code>$mount</code>，可以传入一个DOM节点做为挂载节点，当然也可以不传入参数，后面手动用<code>dom</code>方法把节点插入。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">// 这里返回的messageWithDom依然还是vue实例而不是dom节点，但是这个实例多了一个$el属性，这个属性里面就藏着我们需要挂载的dom节点</span></span><br><span class="line"><span class="keyword">const</span> messageWithDom = messageInstance.$mount()</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> dom = messageWithDom.$el</span><br><span class="line"></span><br><span class="line"><span class="built_in">document</span>.body.appendChild(dom)</span><br></pre></td></tr></table></figure>
<h3 id="野路子"><a href="#野路子" class="headerlink" title="野路子"></a>野路子</h3><p><code>vue</code>隐式化实例，基本是这个套路，当然我在看<em>iview组件库</em>中用了一些其他的野路子，这里也贴一下：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">import</span> Notification <span class="keyword">from</span> <span class="string">'./notification.vue'</span>;</span><br><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">'vue'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> _props = properties || &#123;&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> Instance = <span class="keyword">new</span> Vue(&#123;</span><br><span class="line">    render (h) &#123;</span><br><span class="line">        <span class="keyword">return</span> h(Notification, &#123;</span><br><span class="line">            props: _props</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>道理基本上和我说的差不多，不过调用<code>Vue.extend</code>更加容易理解。</p>
<h3 id="后续控制"><a href="#后续控制" class="headerlink" title="后续控制"></a>后续控制</h3><p>拿到组件的实例后，基本上想怎么玩就能怎么玩了，比如说控制隐藏或显示，可以在组件内部定义一个<code>isShow</code>的<code>data</code>属性，在实例上可以这样用：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">if</span>( xxxx ) &#123;</span><br><span class="line">    messageWithDom.isShow = <span class="literal">true</span></span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    messageWithDom.isShow = <span class="literal">false</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="react实例化"><a href="#react实例化" class="headerlink" title="react实例化"></a>react实例化</h2><p><code>react</code>的实例化和<code>vue</code>稍稍不同，首先引进来的直接就是一个类所以不需要像<code>Vue</code>一样多做一步转换成构造器，其次<code>react</code>是没有类似<code>vue</code>的<code>$mount</code>方法，这也是我一开始很疑惑的地方，后来突然想起来<code>react</code>把组件的挂载方法放到了<code>reactDom</code>这个包里面了。</p>
<h3 id="创建一个虚拟dom"><a href="#创建一个虚拟dom" class="headerlink" title="创建一个虚拟dom"></a>创建一个虚拟dom</h3><p>这里需要调用<code>React.createElement</code>去创建一个虚拟dom，其实<code>vue</code>也能创建一个虚拟dom，参考上面<code>iview</code>的野路子。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">'react'</span></span><br><span class="line"><span class="keyword">import</span> Message <span class="keyword">from</span> <span class="string">'./message.jsx'</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> customProps = &#123;</span><br><span class="line">    <span class="comment">// 传给自组件的一些props</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> Vnode = React.createElment(Message,&#123;<span class="attr">props</span>:customProps&#125;)</span><br></pre></td></tr></table></figure>
<h3 id="挂载并且拿到实例"><a href="#挂载并且拿到实例" class="headerlink" title="挂载并且拿到实例"></a>挂载并且拿到实例</h3><p><code>react</code>没有<code>$mount</code>方法，而是直接调用<code>reactDom</code>的<code>render</code>方法，相当于<code>vue</code>的两步直接一步完成:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">'react'</span></span><br><span class="line"><span class="keyword">import</span> ReactDom <span class="keyword">from</span> <span class="string">'react-dom'</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> containner = <span class="built_in">document</span>.createElement(<span class="string">'div'</span>)</span><br><span class="line"><span class="built_in">document</span>.body.appendChild(containner)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 把虚拟dom传入reactDom.render方法中，第二个参数是挂载的节点，并返回这个组件的实例</span></span><br><span class="line"><span class="keyword">const</span> messageInstance = ReactDom.render(Vnode,containner)</span><br></pre></td></tr></table></figure>
<h3 id="后续的控制"><a href="#后续的控制" class="headerlink" title="后续的控制"></a>后续的控制</h3><p>拿到组件的实例后，基本想怎么玩就能怎么玩了，但是<strong>别忘记了！！！</strong>，<code>react</code>修改<code>state</code>是调用<code>setState</code>，而不像<code>vue</code>直接修改。</p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>学会隐式化创建实例能够很好的看清楚<code>vue</code>和<code>react</code>内部的细节，对提高<code>vue</code>、<code>react</code>大有好处。我的一个项目有个加载条的组件，同时用<code>react</code>和<code>vue</code>都实现了一遍，可以对比学习发现两者的差异，喜欢的可以点个赞~~。</p>
<ul>
<li><a href="https://github.com/limengke123/fantastic-carnival/blob/master/admin/src/components/general/loading-bar/loading-bar.js#L6" target="_blank" rel="noopener">loadingBar的vue实现</a></li>
<li><a href="https://github.com/limengke123/fantastic-carnival/blob/master/client/src/components/common/loading-bar/loading-bar.js#L7" target="_blank" rel="noopener">loadingBar的react实现</a></li>
</ul>
<hr>
<p><img src="/2018/08/03/vue、react隐式实例化/596444702.jpg" alt="Ivan Konstantinovich Aivazovsky – Wave"></p>
<blockquote>
<p>Ivan Konstantinovich Aivazovsky – Wave 1889</p>
</blockquote>

      
    </div>

    

    
    
    

    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/react/" rel="tag"><i class="fa fa-tag"></i> react</a>
          
            <a href="/tags/vue/" rel="tag"><i class="fa fa-tag"></i> vue</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/08/03/vue与react组件对比学习/" rel="next" title="vue与react组件对比学习">
                <i class="fa fa-chevron-left"></i> vue与react组件对比学习
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/08/03/看到的面试题/" rel="prev" title="看到的面试题">
                看到的面试题 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">Mark Lee</p>
              <p class="site-description motion-element" itemprop="description">折腾小久的博客,在 2018-08-01 终于基本成型啦，以后就常驻啦</p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">26</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  <a href="/categories/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">4</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  <a href="/tags/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">20</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          

          

          
          

          
          

          
            
          
          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#vue、react隐式实例化"><span class="nav-number">1.</span> <span class="nav-text">vue、react隐式实例化</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#隐式实例化"><span class="nav-number">1.1.</span> <span class="nav-text">隐式实例化</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#难点"><span class="nav-number">1.2.</span> <span class="nav-text">难点</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#vue实例化"><span class="nav-number">1.3.</span> <span class="nav-text">vue实例化</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#拿到构造器"><span class="nav-number">1.3.1.</span> <span class="nav-text">拿到构造器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#实例化"><span class="nav-number">1.3.2.</span> <span class="nav-text">实例化</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#挂载"><span class="nav-number">1.3.3.</span> <span class="nav-text">挂载</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#野路子"><span class="nav-number">1.3.4.</span> <span class="nav-text">野路子</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#后续控制"><span class="nav-number">1.3.5.</span> <span class="nav-text">后续控制</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#react实例化"><span class="nav-number">1.4.</span> <span class="nav-text">react实例化</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#创建一个虚拟dom"><span class="nav-number">1.4.1.</span> <span class="nav-text">创建一个虚拟dom</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#挂载并且拿到实例"><span class="nav-number">1.4.2.</span> <span class="nav-text">挂载并且拿到实例</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#后续的控制"><span class="nav-number">1.4.3.</span> <span class="nav-text">后续的控制</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#总结"><span class="nav-number">1.5.</span> <span class="nav-text">总结</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Mark Lee</span>

  

  
</div>


  


<!--

  <div class="powered-by">由 <a class="theme-link" target="_blank" rel="external nofollow" href="https://hexo.io">Hexo</a> 强力驱动 v3.7.1</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" rel="external nofollow" href="https://theme-next.org">NexT.Pisces</a> v6.3.0</div>

-->



        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    
	
    

    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=6.3.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=6.3.0"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=6.3.0"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=6.3.0"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=6.3.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=6.3.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=6.3.0"></script>



  



  










  





  

  

  

  

  
  

  

  

  

  

  

</body>
</html>
